<?php

class fileBucket
{

	private $path = false;

	public function __construct( $path )
	{
		if( is_dir( $path ) && is_writable( $path ) ) {
			$this->path = $path;
		}
	}
	
	// returns string
	public function save( $uid, $name, $fromPath )
	{
		while( strlen( $uid ) < 10 ) {
			$uid = '0'.$uid;
		}
		
		$path =	$this->path.'/'.substr( $uid, 0, 2 ).'/';
		
		if( !is_dir( $path ) ) {
			mkdir( $path );
		}

		
		for( $i=2; $i<=6; $i+=2 ) {
			$path .= substr( $uid, $i, 2 ).'/';
			
			if( !is_dir( $path ) ) {
				mkdir( $path );
			}
		}
		
		rename( $fromPath, $path.$name );
		chmod( $path.$name, 0644 );
		
		return $path.$name;
	}
	
	// alias for save()
	public function put( $uid, $name, $fromPath )
	{
		$this->save( $uid, $name, $fromPath );
	}
	
	// returns: path (string)
	public function retrieve( $uid, $noPrefix = false )
	{
		while( strlen( $uid ) < 10 ) {
			$uid = '0'.$uid;
		}
		
		if( $noPrefix === false ) {
			$path =	$this->path.'/';
		} else {
			$path = '';
		}
		

		for( $i=0; $i<=6; $i+=2 ) {
			$path .= substr( $uid, $i, 2 ).'/';
		}

		return $path;	
	}
	
	// returns: bool
	public function delete( $uid )
	{
	
	}
	

}

?>